package com.wmh.baseservice.skd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.skd.pojo.SkdGoodsCategory;
import com.wmh.baseservice.skd.pojo.SkdGoodsLabel;
import com.wmh.baseservice.skd.service.SkdGoodsCategoryService;
import com.wmh.baseservice.skd.mapper.SkdGoodsCategoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author CH
* @description 针对表【skd_goods_category】的数据库操作Service实现
* @createDate 2023-12-24 14:37:07
*/
@Service
public class SkdGoodsCategoryServiceImpl extends ServiceImpl<SkdGoodsCategoryMapper, SkdGoodsCategory>
    implements SkdGoodsCategoryService{
    @Resource
    private SkdGoodsCategoryMapper skdGoodsCategoryMapper;

    /**
     * 根据用户id和商品分类查询是否存在该分类
     * @param usId 用户id
     * @param category 商品分类
     * @return 存在该分类返回true, 不存在返回false
     */
    @Override
    public boolean selectCategoryByNameAndUsId(Long usId, String category) {
        // 创建查询条件
        QueryWrapper<SkdGoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("us_id", usId);
        queryWrapper.eq("category_name", category);

        // 查询数据库
        SkdGoodsCategory existingCategory = skdGoodsCategoryMapper.selectOne(queryWrapper);

        // 如果存在同名标签, 返回 true; 否则, 返回 false
        return existingCategory != null;
    }

    /**
     * 添加商品分类
     * @param usId 用户id
     * @param category 商品分类
     */
    @Override
    public void addGoodsCategory(Long usId, String category) {
        SkdGoodsCategory skdGoodsCategory = new SkdGoodsCategory();
        skdGoodsCategory.setUsId(usId).setCategoryName(category);
        skdGoodsCategoryMapper.insert(skdGoodsCategory);
    }

    /**
     * 查询商品分类列表
     * @param usId 用户id
     * @return 返回商品分类列表
     */
    @Override
    public List<Map<String, Object>> queryCategoryByUsId(Long usId) {
        List<Map<String, Object>> categoeyList = new ArrayList<>();
        QueryWrapper<SkdGoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("us_id", usId);
        List<SkdGoodsCategory> skdGoodsCategories = skdGoodsCategoryMapper.selectList(queryWrapper);
        skdGoodsCategories.forEach(e -> {
            Map<String, Object> categoryMap = new HashMap<>();
            categoryMap.put("id", e.getId());
            categoryMap.put("goodsCategoryName", e.getCategoryName());
            categoeyList.add(categoryMap);
        });
        return categoeyList;
    }

    /**
     * 删除商品分类
     * @param usId 用户id
     * @param category 商品分类
     */
    @Override
    public void deleteCategory(Long usId, String category) {
        QueryWrapper<SkdGoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("us_id", usId);
        queryWrapper.eq("category_name", category);
        skdGoodsCategoryMapper.delete(queryWrapper);
    }
}




